package com.ant.storage.service.impl;

import com.ant.storage.dto.AuditDto;
import com.ant.storage.mapper.AuditListMapper;
import com.ant.storage.pojo.*;
import com.ant.storage.service.*;
import com.ant.storage.vo.AuditVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author melon
 * @since 2022-03-25
 */
@Service
public class AuditListServiceImpl extends ServiceImpl<AuditListMapper, AuditList> implements IAuditListService {


    @Autowired
    IGoodsPoolService goodsPoolService;

    @Autowired
    IPickingService pickingService;

    @Autowired
    IInvoiceInfoService infoService;


    @Autowired
    IPurchaserRecordService recordService;

    @Autowired
    public ServletContext servletContext;

    @Override
    public boolean getAllPassed(Integer isAudit, String str) {
        List<AuditList> list = this.baseMapper.selectList(new QueryWrapper<AuditList>()
                .eq("status", 2)
                .eq("types", 1)
                .eq("process_type", str)
                .eq("parent_id", isAudit));
        List<AuditList> lists = this.baseMapper.selectList(new QueryWrapper<AuditList>()
                .eq("types", 1)
                .eq("process_type", str)
                .eq("parent_id", isAudit));
        if (list.size() == lists.size()) {
            return true;
        }
        return false;
    }

    @Override
    public List<AuditVo> findAuditList(AuditDto dto) {
        return this.baseMapper.findAuditList(dto);
    }

    @Override
    public List<AuditList> finalApproval(String types) {
        UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
        Integer parentid=null;
        if (types.equals("goodsPool")) {
            GoodsPool proposerId = goodsPoolService.getOne(new QueryWrapper<GoodsPool>().eq("proposer_id",
                    userInfo.getNameId()).isNotNull("process_instance_id")
                    .last("ORDER BY id asc LIMIT 1"));
            parentid=proposerId.getId();
        }
        if (types.equals("picking")) {
            Picking picking = pickingService.getOne(new QueryWrapper<Picking>().eq("user_id",
                    userInfo.getNameId())
                    .last("ORDER BY id desc LIMIT 1"));
            parentid=picking.getId();
        }
        if (types.equals("funds")) {
            InvoiceInfo info = infoService.getOne(new QueryWrapper<InvoiceInfo>().eq("create_by",
                    userInfo.getNameId()).eq("Invoice_type","1")
                    .last("ORDER BY id asc LIMIT 1"));
            parentid=info.getId();
        }
        if (types.equals("rent_review")) {
            InvoiceInfo info = infoService.getOne(new QueryWrapper<InvoiceInfo>().eq("create_by",
                    userInfo.getNameId()).eq("Invoice_type","2")
                    .last("ORDER BY id desc LIMIT 1"));
            parentid=info.getId();
        }
        if (types.equals( "planPurchase")) {

        }
        if (types.equals( "payment")) {

        }
        if (types.equals("planPurchase")) {

        }
        if (types.equals("reimbursement")) {
            PurchaserRecord picking = recordService.getOne(new QueryWrapper<PurchaserRecord>().eq("purchaser_id",
                    userInfo.getNameId()).last("ORDER BY id desc LIMIT 1"));
            parentid=picking.getId();
        }
        if (types.equals("reimbursementInfo")) {
            PurchaserRecord picking = recordService.getOne(new QueryWrapper<PurchaserRecord>().eq("purchaser_id",
                    userInfo.getNameId()).eq("Invoice_type","2")
                    .last("ORDER BY id desc LIMIT 1"));
            parentid=picking.getId();
        }
        return this.baseMapper.finalApproval(types, parentid);
    }

    @Override
    public List<AuditList> finalApprovalFunds(String types,Integer id) {
        return this.baseMapper.finalApproval(types, id);
    }

    //    @Override
    //    public Integer selectIsAudit(String processInstanceId, String user_id) {
    //        System.out.println(processInstanceId+"---"+user_id);
    //        GoodsPool goodsPools = goodsPoolService.getOne(new QueryWrapper<GoodsPool>()
    //                .eq("process_instance_id", processInstanceId));
    //        if(goodsPools!=null){
    //            AuditList parentId = this.getOne(new QueryWrapper<AuditList>().eq("parent_id",
    //                    goodsPools.getId()).eq("process_type","goodsPool").eq("types","1")
    //                    .eq("name_id",user_id));
    //            if(parentId!=null){
    //                AuditList auditList = new AuditList();
    //                auditList.setId(parentId.getId());
    //                auditList.setStatus(2);
    //                auditList.setOpinion("1111");
    //                this.updateById(auditList);
    //                if (this.getAllPassed(parentId.getId())) {
    //                    GoodsPool goodsPool = new GoodsPool();
    //                    goodsPool.setApplyStatus(2);
    //                    UpdateWrapper<GoodsPool> wrapper = new UpdateWrapper<>();
    //                    wrapper.eq("process_instance_id","NyxGK4MoQe-6fcACXefAgA03271684217751");
    //                    goodsPoolService.update(goodsPool, wrapper);
    //                    goodsPoolService.getprocessInstanceIdById("NyxGK4MoQe-6fcACXefAgA03271684217751");
    //                }
    //                return  parentId.getId();
    //            }
    //            return  null;
    //        }
    //        return null;
    //    }
    //
}
